/////////////////////////////////////////////////////////////////////////////
// This file is part of the "Java-DAP" project, a Java implementation
// of the OPeNDAP Data Access Protocol.
//
// Copyright (c) 2007 OPeNDAP, Inc.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
/////////////////////////////////////////////////////////////////////////////
using System;
namespace NDODS.DAP.Server
{
	
	/// <summary> Represents a clause which invokes a function that returns a bool value.
	/// 
	/// </summary>
	/// <author>  joew
	/// </author>
	/// <seealso cref="ClauseFactory">
	/// </seealso>
	public class BoolFunctionClause:AbstractClause, ITopLevelClause
	{
		virtual public bool Value
		{
			get
			{
				return value_Renamed;
			}
			
		}
		/// <summary> Returns the server-side function invoked by this clause</summary>
		virtual public BoolFunction Function
		{
			get
			{
				return function;
			}
			
		}
		
		/// <summary> Creates a new BoolFunctionClause.
		/// 
		/// </summary>
		/// <param name="function">The function invoked by the clause
		/// </param>
		/// <param name="children">A list of SubClauses, to be given as arguments
		/// to the function. If all the arguments are constant, the function
		/// clause will be flagged as constant, and evaluated immediatelyx.
		/// </param>
		/// <throws>  DAP2ServerSideException Thrown if either 1) the function does not </throws>
		/// <summary>                        accept the arguments given, or 2) the
		/// clause is constant, and the attempt to Evaluate it fails.
		/// </summary>
		protected internal BoolFunctionClause(BoolFunction function, System.Collections.IList children)
		{
			
			function.CheckArgs(children);
			this.function = function;
			this.children = children;
			this.constant = true;
			System.Collections.IEnumerator it = children.GetEnumerator();
			//UPGRADE_TODO: Method 'java.util.Iterator.hasNext' was converted to 'System.Collections.IEnumerator.MoveNext' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilIteratorhasNext'"
			while (it.MoveNext())
			{
				//UPGRADE_TODO: Method 'java.util.Iterator.next' was converted to 'System.Collections.IEnumerator.Current' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilIteratornext'"
				ISubClause current = (ISubClause) it.Current;
				current.Parent = this;
				if (!current.Constant)
				{
					constant = false;
				}
			}
			if (constant)
			{
				Evaluate();
			}
		}
		
		public virtual bool Evaluate()
		{
			
			if (!constant || !defined)
			{
				defined = true;
				value_Renamed = function.evaluate(children);
			}
			return value_Renamed;
		}
		
		/// <summary> Prints the original string representation of this clause.
		/// For use in debugging.
		/// </summary>
		public override System.String ToString()
		{
			System.Text.StringBuilder buf = new System.Text.StringBuilder();
			buf.Append(function.Name);
			buf.Append("(");
			System.Collections.IEnumerator it = children.GetEnumerator();
			//UPGRADE_TODO: Method 'java.util.Iterator.hasNext' was converted to 'System.Collections.IEnumerator.MoveNext' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilIteratorhasNext'"
			if (it.MoveNext())
			{
				//UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Object.toString' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
				//UPGRADE_TODO: Method 'java.util.Iterator.next' was converted to 'System.Collections.IEnumerator.Current' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilIteratornext'"
				buf.Append(it.Current.ToString());
			}
			//UPGRADE_TODO: Method 'java.util.Iterator.hasNext' was converted to 'System.Collections.IEnumerator.MoveNext' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilIteratorhasNext'"
			while (it.MoveNext())
			{
				buf.Append(",");
				//UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Object.toString' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
				//UPGRADE_TODO: Method 'java.util.Iterator.next' was converted to 'System.Collections.IEnumerator.Current' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilIteratornext'"
				buf.Append(it.Current.ToString());
			}
			buf.Append(")");
			return buf.ToString();
		}
		
		protected internal BoolFunction function;
		
		protected internal bool value_Renamed;
	}
}